﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Liang.Device.Camera;
using Liang.Device.Camera.Action;
using Liang.Device.Model.Camera;

namespace Liang.UI.CameraControl
{
    /// <summary>
    /// 打开右键上下文菜单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    public delegate void OpenContextMenuHandler(object sender, PlayWindowEventArgs args);

    public class PlayWindow : PictureBox
    {
        /// <summary>
        /// 右键菜单事件
        /// </summary>
        public event OpenContextMenuHandler OpenContextMenuEvent;

        /// <summary>
        /// 窗口边框颜色
        /// </summary>
        Pen m_BorderPen = new Pen(Color.Green, 2.0f);

        /// <summary>
        /// 播放参数
        /// </summary>
        object m_cameraParam = null;

        /// <summary>
        /// 窗口序号
        /// </summary>
        int m_nWindowIndex = 0;

        /// <summary>
        /// 播放窗口构造函数
        /// </summary>
        /// <param name="windowIndex"></param>
        /// <param name="parent"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public PlayWindow(int windowIndex, Control parent, int x, int y, int width, int height)
        {
            m_nWindowIndex = windowIndex;
            this.Parent = parent;
            this.Left = x;
            this.Top = y;
            this.Width = width;
            this.Height = height;
        }

        /// <summary>
        /// 播放窗口构造函数
        /// </summary>
        /// <param name="windowIndex"></param>
        /// <param name="parent"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="obj"></param>
        public PlayWindow(int windowIndex, Control parent, int x, int y, int width, int height, object obj)
        {
            m_nWindowIndex = windowIndex;
            this.Parent = parent;
            this.Left = x;
            this.Top = y;
            this.Width = width;
            this.Height = height;

            Init(obj);
        }

        /// <summary>
        /// 初始化播放窗口
        /// </summary>
        /// <param name="obj"></param>
        public void Init(object obj)
        {
            if (!(obj is CameraBase))
            {
                throw new CameraException("输入的摄像头参数不是Camera类型");
            }

            m_cameraParam = obj;
            ((CameraBase)m_cameraParam).PlayWindow = this.Handle;
        }

        /// <summary>
        /// 调整窗口大小
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void ReSizeWindow(int x, int y, int width, int height)
        {
            this.Left = x;
            this.Top = y;
            this.Width = width;
            this.Height = height;

            Invalidate();
        }

        /// <summary>
        /// 打开摄像头
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            if (null == m_cameraParam)
            {
                throw new CameraException("尚未指定摄像头参数");
            }

            Task<bool> result = Task<bool>.Factory.StartNew((obj) =>
            {
                return CameraHelper.Open(obj);
            }, m_cameraParam);

            result.Wait();

            bool ret = result.Result;

            if (!ret)
            {
                throw new CameraException("无法打开摄像头");
            }

            return ret;
        }

        /// <summary>
        /// 打开摄像头
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Open(object obj)
        {
            if (null != m_cameraParam)
            {
                return false;
            }

            m_cameraParam = obj;
            ((CameraBase)m_cameraParam).PlayWindow = this.Handle;

            return CameraHelper.Open(m_cameraParam);
        }

        /// <summary>
        /// 播放视频
        /// </summary>
        /// <returns></returns>
        public bool Play()
        {
            return CameraHelper.Play(m_cameraParam);
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        /// <returns></returns>
        public bool Stop()
        {
            return CameraHelper.Stop(m_cameraParam);
        }

        /// <summary>
        /// 关闭摄像头
        /// </summary>
        /// <returns></returns>
        public bool Close()
        {
            return CameraHelper.Close(m_cameraParam);
        }

        /// <summary>
        /// 抓拍
        /// </summary>
        /// <param name="strImageFile"></param>
        /// <returns></returns>
        public bool CaptureImage(string strImageFile)
        {
            return CameraHelper.Capture(m_cameraParam, strImageFile);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            Size OnePlayWindowSize = this.Size;

            pe.Graphics.DrawRectangle(m_BorderPen, 0, 0, this.Size.Width, this.Size.Height);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (null != OpenContextMenuEvent)
                {
                    OpenContextMenuEvent(this, new PlayWindowEventArgs(m_nWindowIndex));
                }
            }

            base.OnMouseClick(e);
        }
    }

    public class PlayWindowEventArgs : EventArgs
    {
        public int WindowIndex;

        public PlayWindowEventArgs(int windowindex)
        {
            WindowIndex = windowindex;
        }
    }
}
